/*
 * Copyright (c) 2006 Your Corporation. All Rights Reserved.
 */

/**
 * Copyright 2007 Jason Horman, Pete Aykroyd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.functor.expression;

import org.functor.UnaryProcedure;

public class ProcedureVisitor extends ExpressionVisitor {
	private UnaryProcedure<Expression> procedure;

	public ProcedureVisitor(UnaryProcedure<Expression> predicate) {
		this.procedure = predicate;
	}

	public void start(Expression expression) {
		if (expression instanceof Junction)
			visit((Junction)expression);
		else if (expression instanceof Not)
			visit((Not)expression);
		else
			procedure.run(expression);
	}

	public void visit(Junction junction) {
		procedure.run(junction);
		for (PredicateExpression expression : junction.getPredicates())
			start(expression);
	}

	public void visit(Not not) {
		procedure.run(not);
		start(not.getPredicate());
	}

	public static void visit(Expression exp, UnaryProcedure<Expression> procedure) {
		new ProcedureVisitor(procedure).start(exp);
	}
}
